import 'package:ati_package/core/extension/ati_widgets_extension.dart';
import 'package:ati_package/core/routes/app_routes.dart';
import 'package:ati_package/core/utils/ati_assets.dart';
import 'package:ati_package/core/utils/ati_color.dart';
import 'package:ati_package/core/utils/ati_font.dart';
import 'package:ati_package/pages/common/widgets/ati_common_btn.dart';
import 'package:ati_package/pages/common/widgets/ati_text_input.dart';
import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../../common/ati_scaffold.dart';

class WallectWithdrawView extends StatefulWidget {
  const WallectWithdrawView({super.key});

  @override
  State<WallectWithdrawView> createState() => _WallectWithdrawViewState();
}

class _WallectWithdrawViewState extends State<WallectWithdrawView> {
  final _formKey = GlobalKey<FormBuilderState>();

  @override
  Widget build(BuildContext context) {
    return AtiScaffold(
      title: '提现',
      extendBodyBehindAppBar: true,
      barBgColor: Colors.transparent,
      titleColor: Colors.white,
      body: Stack(children: [buildBgImage(), _buildContextWidgets()]),
    );
  }

  Widget buildBgImage() {
    return Container(
      height: 225.r,
      width: Get.width,
      decoration: BoxDecoration(
        image: DecorationImage(
          image: AssetImage(AtiAssets.userWalltBg, package: 'ati_package'),
          fit: BoxFit.fitWidth,
        ),
      ),
    );
  }

  _buildContextWidgets() {
    return SafeArea(
      bottom: false,
      child: SingleChildScrollView(
        child: Container(
          margin: EdgeInsets.only(top: 5.r, left: 16.r, right: 16.r),
          child: Column(
            children: [
              buildHeaderWidgets(),
              _buildTextInput(),
              IncomeExpenseTab(),
              _buildTapAndfootBtn(),
            ],
          ),
        ),
      ),
    );
  }

  //我的积分
  Widget buildHeaderWidgets() {
    return Container(
      padding: EdgeInsets.only(top: 12.5.w, left: 0.r, bottom: 10.r),
      decoration: BoxDecoration(borderRadius: BorderRadius.circular(0.r)),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                '当前余额(USDT)',
                style: TextStyle(
                  fontSize: 12.sp,
                  color: AtiColor.textHLColor1,
                  fontWeight: FontWeight.w500,
                ),
              ),
              SizedBox(height: 5.h),
              Text(
                '29166.00',
                style: TextStyle(
                  fontSize: 30.sp,
                  color: AtiColor.textHLColor1,
                  fontWeight: FontWeight.w600,
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

  _buildTextInput() {
    return Container(
      // padding: EdgeInsets.only(top: 20.r),
      decoration: BoxDecoration(
        color: Colors.white,

        borderRadius: BorderRadius.all(Radius.circular(12.r)),
      ),
      child: FormBuilder(
        key: _formKey,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Container(
              padding: EdgeInsets.only(left: 16.r, right: 12.r, top: 12.r),
              child: Row(
                children: [
                  Text(
                    '提现数量',
                    style: TextStyle(
                      fontSize: AtiFont.font14,
                      color: AtiColor.textColor1,
                      fontWeight: FontWeight.w600,
                    ),
                  ),
                  Spacer(),
                  Text(
                    '全部提出',
                    style: TextStyle(
                      fontSize: AtiFont.font12,
                      color: AtiColor.primary,
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                ],
              ),
            ),
            AtiTextInput(
              name: 'oldPassword',
              padding: EdgeInsets.only(
                top: 10.r,
                left: 16.r,
                right: 10.r,
                bottom: 7.5.r,
              ),
              // label: '提现数量',
              hint: '请输入提现数量',
              showClearButton: true,
              // bgColor: Colors.white,
              prefixLabel: AtiAssets.userUsdtIcon,
              suffixLabel: 'USDT',
              showPrefixIcon: true,
              showPrefixType: true,
              showBorder: false,
              validator: (val) => val == null || val.isEmpty ? '请输入姓名' : null,
            ),
          ],
        ),
      ),
    );
  }

  _buildTapAndfootBtn() {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 16.r, vertical: 15.r),

      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          AtiCommonBtn(
            onPressed: () {},
            titleStr: '提现',
            margin: EdgeInsets.symmetric(horizontal: 10.r, vertical: 32.r),
          ),
        ],
      ),
    );
  }
}

//按键条
class IncomeExpenseTab extends StatefulWidget {
  const IncomeExpenseTab({super.key});

  @override
  State<IncomeExpenseTab> createState() => _IncomeExpenseTabState();
}

class _IncomeExpenseTabState extends State<IncomeExpenseTab> {
  int _selectedIndex = 0; // 0 收入，1 支出
  int _haveWallect = 1; // 有钱包地址

  final List<String> _tabs = ['提现到钱包', '提现到商城'];

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: List.generate(_tabs.length, (index) {
            final bool isSelected = _selectedIndex == index;
            return GestureDetector(
              onTap: () {
                setState(() {
                  _selectedIndex = index;
                  // _haveWallect = index;
                });
              },
              child: AnimatedContainer(
                duration: const Duration(milliseconds: 200),
                margin: EdgeInsets.only(
                  top: 10.r,
                  bottom: 17.5.r,
                  left: 5.r,
                  right: 5.r,
                ),
                padding: EdgeInsets.symmetric(
                  horizontal: 22.5.w,
                  vertical: 10.h,
                ),
                decoration: BoxDecoration(
                  gradient: isSelected
                      ? AtiColor.primaryGradient
                      : LinearGradient(
                          colors: [Colors.white, Colors.white],
                          begin: Alignment.centerLeft,
                          end: Alignment.centerRight,
                        ),
                  borderRadius: BorderRadius.circular(24.r),
                  border: Border.all(
                    color: isSelected ? Color(0xFF059B4F) : Color(0xFF03CC63),
                    width: 1,
                  ),
                ),
                child: Text(
                  _tabs[index],
                  style: TextStyle(
                    color: isSelected ? Colors.white : Color(0xFF03CC63),
                    fontWeight: FontWeight.w500,
                    fontSize: 14.sp,
                  ),
                ),
              ),
            );
          }),
        ),
        _selectedIndex == 0
            ? _haveWallect == 0
                  ? _buildNoWalletAddressWidget()
                  : _buildWithdrawToWallect()
            : _buildMallAccountInput(),

        // _buildWithdrawToWallect(),
      ],
    );
  }

  _buildWithdrawToWallect() {
    return Container(
      padding: EdgeInsets.only(
        top: 16.r,
        bottom: 12.r,
        left: 16.r,
        right: 16.r,
      ),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(8.r),
      ),

      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Text(
                'USDT-TRC20',
                style: TextStyle(
                  fontSize: AtiFont.font14,
                  color: AtiColor.textColor1,
                  fontWeight: FontWeight.w600,
                ),
              ),
              Spacer(),
              Image.asset(
                width: 22.5.r,
                AtiAssets.atiCopyIcon,
                package: 'ati_package',
              ),
            ],
          ),
          SizedBox(height: 10.r),
          Row(
            children: [
              Text(
                'T9z2kvNgUNTxBdvFsqecMUwoJkriGEN5Fh',
                style: TextStyle(
                  fontSize: AtiFont.font12,
                  color: AtiColor.textColor1,
                ),
              ),
              Spacer(),
            ],
          ),
        ],
      ),
    );
  }

  _buildNoWalletAddressWidget() {
    return Container(
      height: 50.r,
      width: double.infinity,
      padding: EdgeInsets.only(left: 16.r),
      alignment: Alignment.centerLeft,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(8.r),
      ),
      child: Text('+添加钱包地址', style: TextStyle(fontSize: AtiFont.font12)),
    ).addTapGesture(
      onTap: () {
        Get.toNamed(AtiAppRoutes.userAddWalletAddress);
      },
    );
  }

  _buildMallAccountInput() {
    return AtiTextInput(
      name: 'oldPassword',
      label: '商城账号',
      hint: '请输入商城账号',
      showClearButton: true,
      validator: (val) => val == null || val.isEmpty ? '请输入商城账号' : null,
    );
  }
}
